import { request } from "../../utils/request";
import config from "../../utils/config";

const app = getApp();
Page({
    data: {
        picUrl: config.picUrl,
        custom: wx.getMenuButtonBoundingClientRect(),
        keyword:'',
        page:1,
        last_page:1,
        list:[],
        keywordList:[],
        remen:[],
        xinju:[],
        bibei:[],
        noResultHide:true,
        goodListHide:true,
        scrollHide:false,
        endHide:true
    },
    onLoad(options) {
        var keywordList = wx.getStorageSync('keywordList')
        this.setData({keywordList:keywordList ? keywordList : []})
        this.getBangList()
    },
    onReachBottom() {

    },
    changeSearch(e){
        this.setData({keyword:e.detail.value})
    },
    search(){
        var keywordList = this.data.keywordList
        if(keywordList.indexOf(this.data.keyword) < 0){
            keywordList.push(this.data.keyword)
            this.setData({keywordList})
            wx.setStorageSync('keywordList',keywordList)
        }
        this.getList()
    },
    clearKeywrod(){
        this.setData({
            keyword:'',
            list:[],
            scrollHide:false,
            goodListHide:true,
            noResultHide:true
        })
    },
    getList(){
        request({
            url:"/miniprogram/good/getSearchGood",
            data:{page:this.data.page,keyword:this.data.keyword}
        }).then(res=>{
            if(this.data.page = 1) this.setData({list:[]})
            this.setData({
                list:this.data.list.concat(res.data),
                last_page:res.last_page,
                goodListHide:false,
                scrollHide:true,
            })
            if(res.data.length == 0){
                console.log(1)
                this.setData({
                    noResultHide:false,
                    endHide:true
                })
            }else if(res.last_page == this.data.page){
                console.log(2)
                this.setData({
                    noResultHide:true,
                    endHide:false
                })
            }

        })
    },
    tapSearch(e){
        this.setData({keyword:e.currentTarget.dataset.value})
        this.getList()
    },
    clearStorage(){
        this.setData({keywordList:[]})
        wx.setStorageSync('keywordList',[])
    },
    getBangList(){
        request({
            url:"/miniprogram/good/getSearchBangList"
        }).then(res=>{
            this.setData({
                remen:res.data.remen,
                xinju:res.data.xinju,
                bibei:res.data.bibei
            })
        })
    },
    naviGood(e){
        var info = wx.getStorageSync('userInfo')
        //authentication_type 认证类型3-发行商，4-商户，5-普通访客
        //is_authentication 是否经过系统认证，0-未认证；1-审核中；2-审核通过；3-审核被拒
        if(info.is_wechat_authentication != 1){
            wx.showModal({
                title:'提示',
                content:"您还未登录或注册，请先登录",
                cancelText:"取消",
                confirmText:"登录",
                success:res=>{
                    if(res.confirm){
                        wx.switchTab({url:"/pages/my/my"})
                    }else{

                    }
                }
            })
            return false
        }
        if(info.authentication_type == 5){
            wx.showModal({
                title:'提示',
                content:"您还未进行身份认证，请先认证您身份信息",
                cancelText:"认证商户",
                confirmColor:"#000",
                confirmText:"认证发行",
                success:res=>{
                    if(res.confirm){
                        wx.navigateTo({url:"/pages/faxingrenzheng/faxingrenzheng"})
                    }else{
                        wx.navigateTo({url:"/pages/shanghurenzheng/shanghurenzheng"})
                    }
                }
            })
            return false;
        }
        if(info.is_authentication != 2){
            config.toast("您的身份审核暂未通过，请等待审核通过",201,()=>{})
            return false
        }
        wx.navigateTo({url:e.currentTarget.dataset.url})
    },
})